********************************************************************************
** 	TITLE:   f3_corrections_survey_balance.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table V: IGNITE Exposure Balance Test: Custody Staff Survey

********************************************************************************
********************************************************************************
		set sortseed 13

use "$output_data/IGNITE_corrections_survey_clean.dta", clear

gen d_female = 1-d_male

gen treatment = d_contact
gen post = !d_work_preignite
la var post "Started Work after IGNITE"
la var d_postcovid "Started Work after Covid"
la var d_female "Female"

local covs  d_female d_black  d_age_1 d_age_2 d_age_3 d_age_4 d_age_5 d_age_6 post d_postcovid


gen mi_any = 0
foreach var in `covs'{
	replace `var' = -99 if `var' == .
	replace mi_any = 1 if `var' == -99 
}


keep if !mi_any

* T test for each cov
foreach v in  `covs'{
	
	qui su `v' if treatment
	local t_m_`v': di %8.3f r(mean)
	local t_m_`v' = subinstr("`t_m_`v''"," ","",.)
	
	qui su `v' if !treatment
	local c_m_`v': di %8.3f r(mean)
	local c_m_`v' = subinstr("`c_m_`v''"," ","",.)
	
	qui su `v'
	local m_`v': di %8.3f r(mean)
	local m_`v' = subinstr("`m_`v''"," ","",.)
	
	qui reg `v' treatment  i.survey_month , r
	local b_`v' : di %8.3f _b[treatment]
	local b_`v' = subinstr("`b_`v''"," ","",.)
	
	
	lincom treatment
	local se_`v': di %8.3f r(se)
	local se_`v' = subinstr("(`se_`v'')"," ","",.)
	
	local p = r(p)
	local sig_`v' " "
	if `p'<0.1{
		local sig_`v' "$ ^{*} $ "
	}
	if `p'<0.05{
		local sig_`v' "$ ^{**} $ "
	}
	if `p'<0.01{
		local sig_`v' "$ ^{***} $ "
	}
	
	local lab_`v': var label `v'
	local b_`v' =  "`b_`v''`sig_`v''"
}

* F test
reg treatment `covs'  i.survey_month
local N = e(N)
di `N'

testparm `covs' 
local f_stat: di %8.3f r(F)
local f_stat = subinstr("`f_stat'"," ","",.)

local f_p: di %8.3f r(p)
local f_p = subinstr("`f_p'"," ","",.)

* Make table
cap file close fh
cap erase "$tables/correctional_survey_balance.tex"
file open fh using "$tables/correctional_survey_balance.tex", write replace

foreach v in `covs'{
	file write fh ///
	"`lab_`v'' & `m_`v'' & `b_`v''& `se_`v'' \\ "_n
}

cap file close fh
macro drop fh

cap file close fh
cap erase "$tables/correctional_survey_balance_f.tex"
file open fh using "$tables/correctional_survey_balance_f.tex", write replace


	file write fh ///
	"\emph{F}-Statistic for Joint Test [\emph{p}-value]  & \multicolumn{3}{c}{`f_stat'[`f_p']} \\ "_n ///
	"Observations &  \multicolumn{3}{c}{`N'} \\ "_n 
cap file close fh
macro drop fh